package logic

import (
	"context"
	"examrpc/examrpc/examrpc"
	"examrpc/examrpc/internal/svc"
	"examrpc/models/mysql"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectLogic) Perfect(in *examrpc.PerfectRequest) (*examrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var finduser mysql.User
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("当前账号未注册，请前去注册")
	}
	user := mysql.User{

		Age:         int(in.Age),
		Form:        in.Form,
		Eamil:       in.Email,
		HeaderImage: in.HederImage,
	}
	mysql.DB.Debug().Where("id = ?", in.UserId).Updates(&user)

	return &examrpc.PerfectResponse{
		Id: int64(finduser.ID),
	}, nil
}
